// 
// Copyright 2010 Classpath Pty Ltd <opensource@classpath.com.au>
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// 

using build

**
** Tests the `MySqlAdapter` class.
**
internal class MySqlAdapterTest : Test {

    **
    ** Stores an instance of `MySqlAdapter` for testing.
    **
    MySqlAdapter? adapter

    **
    ** Stores an instance of `CanCreateColumn` for testing.
    **
    CanCreateColumn? canCreateColumn

    **
    ** Stores an instance of `CanUseColumnOptions` for testing.
    **
    CanUseColumnOptions? canUseColumnOptions

    **
    ** Stores an instance of `CanUseTableOptions` for testing.
    **
    CanUseTableOptions? canUseTableOptions

    **
    ** Stores an instance of `CanDropTableOptions` for testing.
    **
    CanDropTableOptions? canDropTableOptions

    **
    ** Performs additional setup needed for MySqlAdapterTest
    **
    override Void setup() {
        super.setup()
        adapter = MySqlAdapter()
        canCreateColumn = CanCreateColumnTestClass()
        canUseColumnOptions = CanUseColumnOptionsTestClass()
        canUseTableOptions = CanUseTableOptionsTestClass()
        canDropTableOptions = CanDropTableOptionsTestClass()
    }

    **
    ** Cleans up additional setup needed for MySqlAdapterTest
    **
    override Void teardown() {
        super.teardown()
        adapter = null
        canCreateColumn = null
        canUseColumnOptions = null
        canUseTableOptions = null
        canDropTableOptions = null
    }

    **
    ** Tests `MySqlAdapter.createColumnSql`
    **
    Void testCreateColumnSql() {
        verifyEq("column_name INT AUTO_INCREMENT", adapter.createColumnSql(canCreateColumn.createColumn("column_name", canCreateColumn.serial)))
    }

    **
    ** Tests `MySqlAdapter.checkColumnOptionSql`
    **
    Void testCheckColumnOptionSql() {
        option := canUseColumnOptions.check("check_rule")
        expected := Str.defVal
        verifyEq(expected, adapter.checkColumnOptionSql(option))
    }

    **
    ** Tests `MySqlAdapter.checkTableOptionSql`
    **
    Void testCheckTableOptionSql() {
        option := canUseTableOptions.check("ck_name", "check_rule")
        expected := "CHECK (check_rule)"
        verifyEq(expected, adapter.checkTableOptionSql(option))
    }

    **
    ** Tests `MySqlAdapter.dropPrimaryKeyTableOption`
    **
    Void testDropPrimaryKeyTableOption() {
        option := canDropTableOptions.dropPrimaryKey("pk_name")
        expected := "DROP PRIMARY KEY"
        verifyEq(expected, adapter.dropPrimaryKeyTableOptionSql(option))
    }

    **
    ** Tests `MySqlAdapter.sql`.
    **
    Void testMigrationZero() {
        migrationZero := MigrationZero()
        migrationZero.up
        verifyEq(1, migrationZero.modifications.size)
        expected := """CREATE TABLE version_history (
                       vh_id INTEGER AUTO_INCREMENT COMMENT 'The ID of the migration' PRIMARY KEY,
                       vh_timestamp TIMESTAMP COMMENT 'The timestamp of the migration' DEFAULT NOW() NOT NULL,
                       vh_from_version VARCHAR(32) COMMENT 'The version being migrated from' NOT NULL,
                       vh_to_version VARCHAR(32) COMMENT 'The version being migrated to' NOT NULL
                       )"""
        verifyEq(expected, adapter.sql(migrationZero.modifications[0]))
    }

}